package com.uphone.magicleafteacher.util;

import com.radish.baselibrary.utils.LogUtils;
import com.uphone.magicleafteacher.MyApp;
import com.zlw.main.recorderlib.RecordManager;
import com.zlw.main.recorderlib.recorder.RecordConfig.RecordFormat;
import com.zlw.main.recorderlib.recorder.listener.RecordResultListener;

public class RecordUtils {
    private RecordManager mRecordManager;
    private static RecordUtils utils;

    private RecordUtils(RecordManager recordManager) {
        this.mRecordManager = recordManager;
    }

    public static RecordUtils init() {
        if (utils == null) {
            RecordManager recordManager = RecordManager.getInstance();
            recordManager.init(MyApp.getInstance(), true);
            recordManager.changeFormat(RecordFormat.MP3);
            recordManager.changeRecordConfig(recordManager.getRecordConfig().setSampleRate(16000));
            recordManager.changeRecordConfig(recordManager.getRecordConfig().setEncodingConfig(2));
            utils = new RecordUtils(recordManager);
        }

        return utils;
    }

    public void doStop(RecordResultListener listener) {
        LogUtils.e("结束录制音频");
        if (listener != null) {
            this.mRecordManager.setRecordResultListener(listener);
        }
        this.mRecordManager.stop();
    }

    public void doPlay() {
        if (utils == null) {
            init();
        }
        LogUtils.e("开始录制音频");
        this.mRecordManager.start();
    }
}
